package com.guli.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.handler.EduException;
import com.guli.edu.entity.EduCourse;
import com.guli.edu.entity.EduCourseDescription;
import com.guli.edu.entity.EduVideo;
import com.guli.edu.entity.dto.CoursePublishDto;
import com.guli.edu.entity.form.CourseForm;
import com.guli.edu.entity.form.CourseInfoForm;
import com.guli.edu.mapper.EduCourseDescriptionMapper;
import com.guli.edu.mapper.EduCourseMapper;
import com.guli.edu.service.EduChapterService;
import com.guli.edu.service.EduCourseDescriptionService;
import com.guli.edu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.edu.service.EduVideoService;
import com.guli.edu.vo.CourseWebVo;
import com.netflix.discovery.converters.Auto;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author Daniel
 * @since 2019-03-26
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService  ;

    @Autowired
    private EduChapterService eduChapterService  ;

    @Transactional
    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setStatus(EduCourse.COURSE_DRAFT);
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        boolean resultInfo = this.save(eduCourse);
        if (!resultInfo) {
            throw new EduException(200001, "课程信息保存失败");
        }
        //保存课程详情信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(eduCourse.getId());
        courseDescription.setDescription(courseInfoForm.getDescription());
        boolean resultDesciption = eduCourseDescriptionService.save(courseDescription);
        if (!resultDesciption) {
            throw new EduException(20001, "课程详情保存失败");
        }
        return eduCourse.getId();
    }

    @Override
    public CourseInfoForm getCourseById(String id) {
        //1：根据id查询课程基本信息
        EduCourse eduCourse = baseMapper.selectById(id);
        if(eduCourse == null){
            throw new EduException(20001, "数据不存在");
        }
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse,courseInfoForm);


        //2:根据id查询课程描述信息
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        if(eduCourseDescription == null){
            throw new EduException(20001, "数据不存在");
        }
        courseInfoForm.setDescription(eduCourseDescription.getDescription());

        return courseInfoForm;
    }

    @Transactional
    @Override
    public boolean updateCourseById(CourseInfoForm courseInfoForm) {
        //1.修改课程信息表
        EduCourse educourseData = baseMapper.selectById(courseInfoForm.getId());

        BeanUtils.copyProperties(courseInfoForm,educourseData);

        int count = baseMapper.updateById(educourseData);
        if(count ==0 ){
            throw new EduException(20001,"修改课程信息失败");
        }
        //2.修改课程描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseInfoForm.getId());
        courseDescription.setDescription(courseInfoForm.getDescription());
        boolean flag = eduCourseDescriptionService.updateById(courseDescription);
        if (flag) {
            return true ;
        }else{
            return false ;
        }

    }

    @Override
    public CoursePublishDto getCourseInfoId(String id) {
        CoursePublishDto courseInfo = baseMapper.getCourseInfoById(id);

        return courseInfo;
    }

    @Override
    public void publishCourse(String id) {
        //修改课程的状态
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        baseMapper.updateById(eduCourse);
    }

    @Override
    public void pageQuery(Page<EduCourse> pageParam, CourseForm coursForm) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        if (coursForm==null) {
            baseMapper.selectPage(pageParam,wrapper);
            return  ;
        }
        String title = coursForm.getTitle();
        String teacherId = coursForm.getTeacherId();
        String subjectId = coursForm.getSubjectId();
        String subjectParentId = coursForm.getSubjectParentId();
        if (!StringUtils.isEmpty(title)){
            wrapper.eq("title",title);
        }
        if (!StringUtils.isEmpty(teacherId)) {
            wrapper.eq("teacher_id",teacherId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.eq("subject_id",subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq("subject_parent_id",subjectParentId);
        }
        baseMapper.selectPage(pageParam,wrapper);
    }

    @Transactional
    @Override
    public boolean removeCourseById(String id) {
        //根据id删除所有的视频
        eduVideoService.removeCourseId(id);
        //根据id删除所有的章节
        eduChapterService.removeByCourseId(id);
        //根据id删除所有的课程详情
        eduCourseDescriptionService.removeById(id);
        //删除封面
        //TODO
        Integer result = baseMapper.deleteById(id);
        return null != result  && result >0   ;
    }

    @Override
    public List<EduCourse> selectByTeacherId(String teacherId) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper();
        wrapper.eq("teacher_id",teacherId);
        //按照最后更新时间倒叙排列
        wrapper.orderByDesc("gmt_modified");
        List<EduCourse> courses = baseMapper.selectList(wrapper);
        return courses;
    }

    @Override
    public Map<String, Object> getPageList(Long page, Long limit) {
        //
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("status","Normal");
        baseMapper.selectPage(pageCourse,wrapper);
        //从pageCourse获取分页的数据
        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long total = pageCourse.getTotal();
        long size = pageCourse.getSize();
        long pages = pageCourse.getPages();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();

        Map<String ,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("total",total);
        map.put("pages",pages);
        map.put("size",size);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map;
    }

    @Transactional
    @Override
    public CourseWebVo selectInfoWebById(String id) {
        this.updatePageViewCount(id);
        return baseMapper.selectInfoWebById(id);
    }

    @Override
    public void updatePageViewCount(String id) {
        EduCourse eduCourse = baseMapper.selectById(id);
        eduCourse.setViewCount(eduCourse.getViewCount()+1);
        baseMapper.updateById(eduCourse);
    }


}
